﻿using System.Collections.Generic;
using QFramework;
using UnityEngine;

namespace Survivor
{
    public class CoinUpgradeSystem : AbstractSystem, ICanSave
    {
        public readonly List<CoinUpgradeItem> CoinUpgradeItemList = new List<CoinUpgradeItem>();
        public static EasyEvent OnCoinUpgradeSystemChanged = new EasyEvent();

        protected override void OnInit()
        {
            var coinPercentLv1 = Add(new CoinUpgradeItem()
                .WithKey("coin_percent_lv1")
                .WithDescribe("金币掉落概率提升lv1")
                .WithPrice(5)
                .OnUpgrade(item =>
                {
                    Global.Coin.Value -= item.Price;
                    Global.CoinDropPercent.Value += 0.1f;
                }));
            var coinPercentLv2 = Add(new CoinUpgradeItem()
                .WithKey("coin_percent_lv2")
                .WithDescribe("金币掉落概率提升lv2")
                .WithPrice(7)
                .OnCondition(item => coinPercentLv1.UpgradeFinish)
                .OnUpgrade(item =>
                {
                    Global.Coin.Value -= item.Price;
                    Global.CoinDropPercent.Value += 0.1f;
                }));
            coinPercentLv1.OnChange.Register(() =>
            {
                coinPercentLv2.OnChange.Trigger();
            });
            var coinPercentLv3 = Add(new CoinUpgradeItem()
                .WithKey("coin_percent_lv3")
                .WithDescribe("金币掉落概率提升lv3")
                .WithPrice(10)
                .OnCondition(item => coinPercentLv2.UpgradeFinish)
                .OnUpgrade(item =>
                {
                    Global.Coin.Value -= item.Price;
                    Global.CoinDropPercent.Value += 0.1f;
                }));
            coinPercentLv2.OnChange.Register(() =>
            {
                coinPercentLv3.OnChange.Trigger();
            });
            
            CoinUpgradeItemList.Add(new CoinUpgradeItem()
                .WithKey("exp_percent")
                .WithDescribe("经验掉落概率提升")
                .WithPrice(5)
                .OnUpgrade(item =>
                {
                    Global.Coin.Value -= item.Price;
                    Global.ExpDropPercent.Value += 0.1f;
                }));
            
            CoinUpgradeItemList.Add(new CoinUpgradeItem()
                .WithKey("max_hp_percent")
                .WithDescribe("最大生命值+1")
                .WithPrice(20)
                .OnUpgrade(item =>
                {
                    Global.Coin.Value -= item.Price;
                    Global.MaxHp.Value++;
                }));
            
            Load();
            OnCoinUpgradeSystemChanged.Register(Save);
        }
        
        private CoinUpgradeItem Add(CoinUpgradeItem item)
        {
            CoinUpgradeItemList.Add(item);
            return item;
        }

        public void Say()
        {
            Debug.Log("Hello CoinUpgradeSystem");
        }

        public void Save()
        {
            var saveSystem = this.GetSystem<SaveSystem>();
            foreach (var coinUpgradeItem in CoinUpgradeItemList)
            {
                saveSystem.SetBool(coinUpgradeItem.Key, coinUpgradeItem.UpgradeFinish);
            }
        }

        public void Load()
        {
            var saveSystem = this.GetSystem<SaveSystem>();
            foreach (var coinUpgradeItem in CoinUpgradeItemList)
            {
                coinUpgradeItem.UpgradeFinish = saveSystem.GetBool(coinUpgradeItem.Key, false);
            }
        }
    }
}